<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <title>Normal / Learn Vimscript the Hard Way</title>
        <meta name="description" content="">
        <meta name="author" content="Steve Losh">
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

        <link href="/static/styles/skeleton/base.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/skeleton.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/layout.css" rel="stylesheet" type="text/css" />

        <link href="/static/styles/tango.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/style.less" rel="stylesheet/less" type="text/css" />

        <script type="text/javascript" src="/static/scripts/less.js"></script>
    </head>

    <body class="">
        <div class="container">
            <header class="sixteen columns">
                <h1><a href="/">Learn Vimscript the Hard Way</a></h1>
            </header>

            
    <section class="nav three columns">
        
<ul>
<li><a href="#normal">Normal</a><ul>
<li><a href="#avoiding-mappings">Avoiding Mappings</a></li>
<li><a href="#special-characters">Special Characters</a></li>
<li><a href="#exercises">Exercises</a></li>
<li><a href="#extra-credit">Extra Credit</a></li>
</ul>
</li>
</ul>


        <div class="prevnext">
            
                <a class="prev" href="28.html">&laquo; Prev</a>
            
            
                <a class="next" href="30.html">Next &raquo;</a>
            
        </div>
    </section>

    <section class="content twelve columns offset-by-one">
        <div> 
<h1 id="normal">Normal</h1>
<p>So far we've covered some of the most useful Vimscript commands, but what about
all the stuff you do on a daily basis in normal mode?  Can we somehow use all
the knowledge we have from editing text in our scripting?</p>
<p>The answer is: "of course".  We've seen the <code>normal</code> command before, and now
it's time to revisit it in a bit more detail.  Run the following command:</p>
<pre class="codehilite"><code class="language-vim">:normal G</code></pre>


<p>Vim will move your cursor to the last line in the current file, just like
pressing <code>G</code> in normal mode would.  Now run the following command:</p>
<pre class="codehilite"><code class="language-vim">:normal ggdd</code></pre>


<p>Vim will move to the first line in the file (<code>gg</code>) and then delete it (<code>dd</code>).</p>
<p>The <code>normal</code> command simply takes a sequence of keys and pretends they were
typed in normal mode.  Seems simple enough.</p>
<h2 id="avoiding-mappings">Avoiding Mappings</h2>
<p>Run the following command to map the <code>G</code> key to something else:</p>
<pre class="codehilite"><code class="language-vim">:nnoremap G dd</code></pre>


<p>Now pressing <code>G</code> in normal mode will delete a line.  Try this command:</p>
<pre class="codehilite"><code class="language-vim">:normal G</code></pre>


<p>Vim will delete the current line.  The <code>normal</code> command will take into account
any mappings that exist.</p>
<p>This means that we need something like the <code>nnoremap</code> version of <code>nmap</code> for
<code>normal</code>, otherwise we'll never be able to use it since we can't know what keys
our users have mapped.</p>
<p>Luckily Vim has a <code>normal!</code> command that does exactly this.  Run this command:</p>
<pre class="codehilite"><code class="language-vim">:normal! G</code></pre>


<p>This time Vim moves to the bottom of the file even though <code>G</code> has been mapped.</p>
<p>When writing Vim scripts you should <strong>always</strong> use <code>normal!</code>, and <strong>never</strong> use
plain old <code>normal</code>.  You can't trust what keys your users will have mapped in
their <code>~/.vimrc</code> files.</p>
<h2 id="special-characters">Special Characters</h2>
<p>If you play around with <code>normal!</code> long enough you'll probably notice a problem.
Try the following command:</p>
<pre class="codehilite"><code class="language-vim">:normal! /foo&lt;cr&gt;</code></pre>


<p>At first glance it may seem like this should perform a search for <code>foo</code>, but
you'll see that it doesn't work.  The problem is that <code>normal!</code> doesn't parse
special character sequences like <code>&lt;cr&gt;</code>.</p>
<p>In this case Vim thinks you wanted to search for the character sequence "f, o,
o, left angle bracket, c, r, right angle bracket", and doesn't realize that you
even pressed return to perform the search!  We'll talk about how to get around
this in the next chapter.</p>
<h2 id="exercises">Exercises</h2>
<p>Read <code>:help normal</code>.  The end of it will hint at the topic of the next chapter.</p>
<h2 id="extra-credit">Extra Credit</h2>
<p>If you're not feeling up for a challenge, skip this section.  If you are, good
luck!</p>
<p>Recall what <code>:help normal</code> said about undo.  Try to make a mapping that will
delete two lines but let you undo each deletion separately.  <code>nnoremap &lt;leader&gt;d
dddd</code> is a good place to start.</p>
<p>You won't actually need <code>normal!</code> for this (<code>nnoremap</code> will suffice), but it
illustrates a good point: sometimes reading about one Vim command can spark an
interest in something unrelated.</p>
<p>If you've never used the <code>helpgrep</code> command you'll probably need it now.  Read
<code>:help helpgrep</code>.  Pay attention to the parts about how to navigate between the
matches.</p>
<p>Don't worry about patterns yet, we're going to cover them soon.  For now it's
enough to know that you can use something like <code>foo.*bar</code> to find lines
containing that regex in the documentation.</p>
<p>Unfortunately <code>helpgrep</code> can be frustrating at times because you need to know
what words to search for before you can find them!  I'll cut you some slack and
tell you that in this case you're looking for a way to break Vim's undo sequence
manually, so that the two deletes in your mapping can be undone separately.</p>
<p>In the future, be pragmatic.  Sometimes Google is quicker and easier when you
don't know exactly what you're after.</p></div>

        <div class="prevnext">
            
                <a class="prev" href="28.html">&laquo; Previous</a>
            
            
                <a class="next" href="30.html">Next &raquo;</a>
            
        </div>
    </section>


            <footer class="sixteen columns">
                Made by <a href="http://stevelosh.com">Steve Losh</a>.

                <a href="/license.html">License</a>.

                Built with
                <a href="http://bitbucket.org/sjl/bookmarkdown/">Bookmarkdown</a>.
            </footer>
        </div>

        
            <script type="text/javascript">
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-15328874-8']);
                _gaq.push(['_trackPageview']);

                (function() {
                 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                 })();
            </script>
        

        
            <script type="text/javascript">
                var _gauges = _gauges || [];
                (function() {
                 var t   = document.createElement('script');
                 t.type  = 'text/javascript';
                 t.async = true;
                 t.id    = 'gauges-tracker';
                 t.setAttribute('data-site-id', '4e8f83b7f5a1f546e200000d');
                 t.src = '//secure.gaug.es/track.js';
                 var s = document.getElementsByTagName('script')[0];
                 s.parentNode.insertBefore(t, s);
                 })();
             </script>
        
    </body>
</html>
